/**
 * @author angelo gimenez
 */
//sample input 
/*
new CC_FW.modules.ListView({
    numbered: true, 
    filter: true, 
    inset: true,
    listItems: [
        {
            content: '<h1>test 1</h1>',
            isDivider: false,
            isField: false,
            filterText: 'test 1'
        },
        {
            content: '<h1>test 2</h1>',
            isDivider: false,
            isField: false,
            filterText: 'test 2'
        },
        {
            content: '<h1>test 3</h1>',
            isDivider: false,
            isField: false,
            filterText: 'test 3'
        },
        {
            content: '<h1>test 4</h1>',
            isDivider: false,
            isField: false,
            filterText: 'test 4'
        }
     ]
});
*/
CC_FW.modules.ListView = Backbone.View.extend({
	tagName: 'ul',
	inset: false,
	filter: false,
	numbered: false,
	listItems: new Array(),
	listItemObjs: new Array(),
	initialize: function(options){
		this.numbered = typeof options.numbered == 'undefined' || options.numbered == null? false: options.numbered;
		this.filter = typeof options.filter == 'undefined' || options.filter == null? false: options.filter;
		this.filterPlaceHolder = CC_FW.utils.isDefined(options.filterPlaceHolder)? options.filterPlaceHolder: null;
		this.inset = typeof options.inset == 'undefined' || options.inset == null? false: options.inset;
		this.listItems = typeof options.listItems == 'undefined' || options.listItems == null? new Array(): options.listItems;
	},
	render: function(){
		$(this.el).html('');
		for(var i=0;i<this.listItems.length;i++){
			var listItemObj = this.listItems[i];
			var listViewItem = new CC_FW.modules.ListViewItem({
				content: listItemObj.content,
				filterText: listItemObj.filterText,
				isDivider: listItemObj.isDivider,
				isField: listItemObj.isField
			}).render();
			$(this.el).append(listViewItem.el);
			this.listItemObjs.push(listViewItem);
		}
		this.addMarkupAttributes();
		this.$el.trigger('create');
		//$(this.el).listview('refresh');
		return this;
	},
	addMarkupAttributes: function(){
		$(this.el).attr('data-role', "listview");
		$(this.el).attr('data-inset', this.inset);
		$(this.el).attr('data-filter', this.filter);
		if(CC_FW.utils.isDefined(this.filterPlaceHolder)) $(this.el).attr('data-filter-placeholder', this.filterPlaceHolder);
		
		return true;
	}
});

CC_FW.modules.ListViewItem = Backbone.View.extend({
	tagName: 'li',
	filterText: null,
	content: null,
	isDivider: false,
	isField: false,
	initialize: function(options){
		this.content = typeof options.content == 'undefined' || options.content == null? '&gt;empty&lt;': options.content;
		this.filterText = typeof options.filterText == 'undefined' ? null: options.filterText;
		this.isDivider = typeof options.isDivider == 'undefined' || options.isDivider == null? false: options.isDivider;
		this.isField = typeof options.isField == 'undefined' || options.isField == null? false: options.isField;
	},
	render: function(){
		this.addMarkupAttributes();
		$(this.el).html(this.content);
		$(this.el).append('<div style="clear:both;"></div>');
		return this;
	},
	addMarkupAttributes: function(){
		if(this.filterText != null) $(this.el).attr('data-filtertext', this.filterText);
		if(this.isDivider == true){
		   $(this.el).attr('data-role', 'list-divider');
		   $(this.el).attr('data-theme', "a"); 
		} 
	}
});
